package nl.hanze.designpatterns.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import nl.hanze.designpatterns.DAO.impl.db.TroubleTicketDAOImpl;
import nl.hanze.designpatterns.domain.TroubleTicket;

public class SearchTroubleTicketsPanel extends JPanel{
	List<TroubleTicket> list;
	JPanel insidepanel;
	JTextField field;
	JPanel searchpanel;
	JScrollPane listpanel;
	JTable table = null;
	/**
	 * 
	 */
	private static final long serialVersionUID = 7937563267387078318L;

	public SearchTroubleTicketsPanel(){
		super();
		list = new ArrayList<TroubleTicket>();
		buildPanel();
	}
	
	public void buildPanel(){
		insidepanel = new JPanel(new BorderLayout());
		searchpanel = new JPanel(new FlowLayout());
		field = new JTextField(10);
		JButton getbutton = new JButton("Submit");
		
		
		
		
		
		getbutton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(table!=null){
				table.removeAll();
				}
				String callername = field.getText();
				String[] columnNames = {"Callername","Description", "Date/time",};
				try {
					list = getTroubleTickets(callername);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				int[][] multi = new int[5][];
				multi[0] = new int[10];
				Object [][] data = new Object[list.size()][3];
				for(int i =0;i<list.size();i++){
					System.out.println(i);
					for(int j=0;j<3;j++){
						data[i]= new Object[]{list.get(i).getCallerName(),list.get(i).getDescription(),list.get(i).getDateTime()};
					}
				}
				JTable table = new JTable(data,columnNames);
				table.setEnabled(false);
				table.getTableHeader().setReorderingAllowed(false);
				listpanel = new JScrollPane(table);
				table.setFillsViewportHeight(true);
				insidepanel.add(listpanel);
				insidepanel.revalidate();
			}
		});

		searchpanel.add(field);
		searchpanel.add(getbutton);
		this.setLayout(new BorderLayout());
		this.add(searchpanel, BorderLayout.PAGE_START);
		this.add(insidepanel, BorderLayout.CENTER);

	}
	
	public List<TroubleTicket> getTroubleTickets(String name) throws Exception{
		TroubleTicketDAOImpl ttdi = new TroubleTicketDAOImpl();
		list = ttdi.getTroubleTicketsByName(name);
		return list;
	}
	
}
